//
//  SchoolIsOverSettings.m
//  SchoolIsOver
//
//  Created by liu ming on 11-6-16.
//  Copyright 2011 peixin. All rights reserved.
//

#import "SchoolIsOverSettings.h"


@implementation SchoolIsOverSettings

@synthesize schoolName;
@synthesize gradeOneCount, gradeTwoCount, gradeThreeCount, gradeFourCount, gradeFiveCount, gradeSixCount;
@synthesize serviceAddress;

@synthesize settings;

-(void)loadSettings
{
	NSString *plistSettingsFileName = [[self documentsPath] stringByAppendingPathComponent:@"Settings.plist"];
	if ([[NSFileManager defaultManager] fileExistsAtPath:plistSettingsFileName]) {
		NSDictionary *dict = [[NSDictionary alloc] initWithContentsOfFile:plistSettingsFileName];
		self.settings = [NSMutableDictionary dictionaryWithDictionary:dict];
		
		[dict release];
	}else {
		NSString *pListPath = [[NSBundle mainBundle] pathForResource:@"Settings" ofType:@"plist"];
		NSDictionary *dict = [[NSDictionary alloc] initWithContentsOfFile:pListPath];
		[dict writeToFile:plistSettingsFileName atomically:YES];
		self.settings = [NSMutableDictionary dictionaryWithDictionary:dict];
		[dict release];
	}
	
	schoolName = [self.settings valueForKey:@"schoolName"];
	//NSLog(@"%@", schoolName);
	gradeOneCount = [self.settings valueForKey:@"gradeOneCount"];
	//NSLog(@"%@",gradeOneCount);
	gradeTwoCount = [self.settings valueForKey:@"gradeTwoCount"];
	//NSLog(@"%@",gradeTwoCount);
	gradeThreeCount = [self.settings valueForKey:@"gradeThreeCount"];
	//NSLog(@"%@",gradeThreeCount);
	gradeFourCount = [self.settings valueForKey:@"gradeFourCount"];
	//NSLog(@"%@",gradeFourCount);
	gradeFiveCount = [self.settings valueForKey:@"gradeFiveCount"];
	//NSLog(@"%@",gradeFiveCount);
	gradeSixCount = [self.settings valueForKey:@"gradeSixCount"];
	//NSLog(@"%d",[gradeSixCount intValue] );
	serviceAddress = [self.settings valueForKey:@"serviceAddress"];
	//NSLog(@"%@", serviceAddress);
}

-(void)save
{
	NSString *plistSettingsFileName = [[self documentsPath] stringByAppendingPathComponent:@"Settings.plist"];
	
	[settings setObject:schoolName forKey:@"schoolName"];
	
	[settings setObject:gradeOneCount forKey:@"gradeOneCount"];
	[settings setObject:gradeTwoCount forKey:@"gradeTwoCount"];
	[settings setObject:gradeThreeCount forKey:@"gradeThreeCount"];
	[settings setObject:gradeFourCount forKey:@"gradeFourCount"];
	[settings setObject:gradeFiveCount forKey:@"gradeFiveCount"];
	[settings setObject:gradeSixCount forKey:@"gradeSixCount"];
	
	[settings setObject:serviceAddress forKey:@"serviceAddress"];
	

	[settings writeToFile:plistSettingsFileName atomically:YES];
}

-(id) init
{
	self = [super init];
	
	if (self != nil) {
		[self loadSettings];
	}
	
	return self;
}

#pragma mark -
#pragma mark 获取Document文件夹
-(NSString *) documentsPath
{
	NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
	NSString *documentsDir = [paths objectAtIndex:0];
	return documentsDir;
}



-(void) dealloc
{
	[settings release];
	
	[schoolName release];
	
	[gradeOneCount release];
	[gradeTwoCount release];
	[gradeThreeCount release];
	[gradeFourCount release];
	[gradeFiveCount release];
	[gradeSixCount release];
	
	[serviceAddress release];
	
	[super dealloc];
}
@end
